package com.tangyuan.common.util.gaodemap;


import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.math.BigDecimal;

public class GaodeMapUtil {

    private static final String API_URL = "https://restapi.amap.com/v3/geocode/regeo?parameters";
    private static final String API_KEY = "2d87d82d4b9e865429000f789a3d06b5";

    public static final String STATUS_OK = "1";
    public static final String STATUS = "status";
    public static final String REGEO_CODE = "regeocode";
    public static final String ADDRESS_COMPONENT = "addressComponent";
    public static final String AD_CODE = "adcode";

    public static String getCityCode(BigDecimal longitude, BigDecimal latitude) {
        String result = HttpUtil.get(API_URL + "&key=" + API_KEY + "&location=" + longitude + "," + latitude);
        JSONObject entries = JSONUtil.parseObj(result);
        if (STATUS_OK.equals(entries.get(STATUS).toString())) {
            try {
                return entries.getJSONObject(REGEO_CODE).getJSONObject(ADDRESS_COMPONENT).get(AD_CODE).toString();
            } catch (Exception e) {
                System.out.println("==============================【GaodeMapUtil】获取请求结果失败！==============================");
            }
        }
        return null;
    }

    public static void main(String[] args) {
        getCityCode(new BigDecimal("116.397428"), new BigDecimal("39.90923"));
        getCityCode(new BigDecimal("120.071678"), new BigDecimal("36.012607"));
    }
}